﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Service;
using System.Web.UI.HtmlControls;

namespace Web.User
{
    public partial class MyOrder : UserBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测用户登录
            ServiceFactory.CreateInstance<UsersService>().CheckUserLogin();
            if (!Page.IsPostBack)
            {
                OrderCondition condition = new OrderCondition();

                condition.UserID = this.userInfo.UserID;

                this.TextBind(condition);
            }

            btnPayroll.Click += new EventHandler(btnPayroll_Click);
            btnComplate.Click += new EventHandler(btnComplate_Click);
        }

        void btnPayroll_Click(object sender, EventArgs e)
        {
            int flags = 0;
            int orderID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    orderID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                OrderCondition condition= new OrderCondition();
                condition.IsPayroll = YesNo.Yes;
                ServiceFactory.CreateInstance<OrderService>().UpdateOrderByCondition(orderID, condition);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一条");
            }
            else
            {
                base.ResponseErch("请选择一条资讯");
            }
        }

        void btnComplate_Click(object sender, EventArgs e)
        {
            int flags = 0;
            int orderID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    orderID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                OrderCondition condition= new OrderCondition();
                condition.IsComplate = YesNo.Yes;
                ServiceFactory.CreateInstance<OrderService>().UpdateOrderByCondition(orderID, condition);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一条");
            }
            else
            {
                base.ResponseErch("请选择一条资讯");
            }
        }

        private void TextBind(OrderCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<OrderHeaderInfo> list = ServiceFactory.CreateInstance<OrderService>().GetOrderList(ref ps, condition);

            if (list.Count > 0)
            {
                this.rptList.DataSource = list;
                this.rptList.DataBind();

                phHasInfomation.Visible = true;
                phNoInfomation.Visible = false;
            }
            else
            {
                phHasInfomation.Visible = false;
                phNoInfomation.Visible = true;
            }
            //divPage.InnerHtml = ClientPager.CreatePager(ps);
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            OrderHeaderInfo order = (OrderHeaderInfo)e.Item.DataItem;
            Literal litCode = (Literal)e.Item.FindControl("litCode");
            Literal litProducts = (Literal)e.Item.FindControl("litProducts");
            Literal litCount = (Literal)e.Item.FindControl("litCount");
            Literal litPrice = (Literal)e.Item.FindControl("litPrice");
            Literal litAddDate = (Literal)e.Item.FindControl("litAddDate");

            litCode.Text = order.OrderCode;
            string strProducts = string.Empty;
            string strCount = string.Empty;
            string strPrice = string.Empty;
            List<OrderItemInfo> list = ServiceFactory.CreateInstance<OrderService>().GetOrderItemByOrderID(order.HeaderID);
            if (list.Count > 0)
            {
                foreach (OrderItemInfo item in list)
                {
                    strProducts += "<a href='/Product/Product_View.aspx?ProductID=" + item.ProductID + "' class='l256ebe'>" + item.Product.Title + "</a><br />";
                    strCount += item.Count.ToString() + "<br />";
                    strPrice += "￥" + item.Price.ToString("0.00") + "<br />";
                }

                litProducts.Text = strProducts;
                litCount.Text = strCount;
                litPrice.Text = strPrice;
            }

            litAddDate.Text = order.AddDate.ToString("yyyy-MM-dd");
        }
    }
}